package com.tsing.es.config;

import com.alibaba.fastjson.JSON;
import com.tsing.es.domain.User;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.message.BasicHeader;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


import java.io.IOException;
import java.util.Base64;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class ElasticSearchClientConfig {


    //配置RestHighLevelClient依赖到spring容器中待用
    @Bean
    public RestHighLevelClient restHighLevelClient(){
        Header[] hears =  new Header[]{new BasicHeader("Authorization", "Basic " + Base64.getEncoder().encodeToString("elastic:M6yfDXaRDE*L*vSTMxQa".getBytes()))};
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        //绑定本机，端口，协议，如果是ES集群，就配置多个
                        new HttpHost("10.8.67.226",9200,"http"))
                        .setDefaultHeaders(hears)
                );

        return client;
    }


    public static void main(String[] args) throws IOException {
        // 创建Elasticsearch客户端
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("120.46.52.26", 9200, "http")));

     /*   // 插入数据
        insertMethod(client);*/

        // 查询数据
        queryMethod(client);


    }

    /**
     * 查询数据的方法
     * @param client
     * @throws IOException
     */
    private static void queryMethod(RestHighLevelClient client) throws IOException {
        GetRequest getRequest = new GetRequest("my_index", "1");
        GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
        if (getResponse.isExists()) {
            System.out.println("Document found: " + getResponse.getSourceAsString());
        } else {
            System.out.println("Document not found");
        }
        // 关闭客户端连接
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 插入数据
     * @param client
     */
    private static void insertMethod(RestHighLevelClient client) {
        // 准备要插入的数据
        Map<String, Object> jsonMap = new HashMap<>();
        jsonMap.put("name", "WQP");
        jsonMap.put("age", 27);
        jsonMap.put("city", "China");

        // 创建IndexRequest对象
        IndexRequest request = new IndexRequest("wqp"); // 指定索引名称
        request.id("3"); // 可选：指定文档ID
        request.source(jsonMap, XContentType.JSON); // 设置要插入的数据

        // 执行插入操作
        try {
            IndexResponse response = client.index(request, RequestOptions.DEFAULT);
            System.out.println("插入成功，响应信息：" + response.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭客户端连接
            try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}


